package com.etc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author xing
 * @Description
 * @Date 2021/3/8  19:33
 **/
public class HDFSTest {

    private static FileSystem fileSystem;
    public static void init(){
        Configuration configuration = new Configuration();
        try {
            fileSystem = FileSystem.get(new URI("hdfs://192.168.66.90:9000"),configuration,"root");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
          init();
          //日志文件
          String logFilePath = "E:\\log\\servicelog";
          //等待上传目录
          String waitFilePath = "E:\\log\\waitlog";
          //备份目录
          String backupPath = "E:\\log\\backlog";
          //hdfs
          String hdfsFilePath = "/www/";

        // 获取要上传的文件，上传除了service.log外的所有日志文件
        File[] files = new File(logFilePath).listFiles(pathname -> {
            String name = pathname.getName();
            return "service.log".equals(name) ? false : true;
        });

        SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
        String todayStr = simple.format(new Date());
        String todayUploadPath = waitFilePath + todayStr + "/";
        File file = new File(todayUploadPath);
        if(!file.exists()){
            file.mkdirs();
        }
        for(File logFile : files){
            String logFileName = logFile.getName();
            logFile.renameTo(new File(todayUploadPath + "/" + logFileName));
        }
        // 上传到hdfs
        File[] uploadLogFiles = new File(todayUploadPath).listFiles();
        for(File uploadLogFile : uploadLogFiles){
            String uploadLogFilePath = uploadLogFile.getAbsolutePath();
            String uploadLogFileName = uploadLogFile.getName();
            try {
                fileSystem.copyFromLocalFile(new Path(uploadLogFilePath), new Path(hdfsFilePath + todayStr +"/"+ uploadLogFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 拷贝到备份目录
        String todayBackupPath = backupPath + todayStr + "/";
        File todayBackLogFile = new File(todayBackupPath);
        if(!todayBackLogFile.exists()){
            todayBackLogFile.mkdirs();
        }
        for (File backLogFile : uploadLogFiles){
            String backLogFileName = backLogFile.getName();
            backLogFile.renameTo(new File(todayBackupPath + backLogFileName));
        }

    }

}
